﻿using AmiDraw.Editor.Factory;
using AmiDraw.Editor.View;
using AmiDraw.Editor.View.Dialog;
using AmiDraw.Lib;
using Panuon.UI.Core;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO.Compression;
using AmiDraw.Lib.Factory;
using Microsoft.Win32;

namespace AmiDraw.Editor.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class HomeViewModel
    {

        public ObservableCollection<string> Projects { get; set; } = new();


        public RelayCommand OpenProjCommand { get; set; }

        public HomeViewModel()
        {
            OpenProjCommand = new RelayCommand(this.OpenProject);
        }

        public void Load()
        {
            LoadProjects();
        }


        private void LoadProjects()
        {
            var dir = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "projects"));

            FileFactory.InitDir(dir.FullName);


            Projects = new(dir.GetDirectories().Select(x => x.Name));
        }


        /// <summary>
        /// 按钮_创建新的Pack
        /// </summary>
        public void CreateNewPack()
        {
            var dir = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "projects"));

            FileFactory.InitDir(dir.FullName);

            CreatePackDialog dialog = new();

            if (dialog.ShowDialog() == true)
            {
                var packinfo = dialog.ViewModel;

                if (Directory.Exists(Path.Combine(dir.FullName, packinfo.Name)))
                {
                    MessageBox.Show($"已存在{packinfo.Name}这个项目了!");
                    return;
                }

                var info = dir.CreateSubdirectory(packinfo.Name);

                JsonFactory.WriteJson(Path.Combine(info.FullName, "info.json"), packinfo);
                OpenProject(info.FullName);
            }

        }

        public void OpenProject(object projPath)
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "projects", projPath as string);


            FileFactory.InitDir(path);

            (App.Current.MainWindow as MainWindow).ViewModel.Content = new PackEditView(new(path));

        }


        public void ImportPack()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            if (openFile.ShowDialog() == true)
            {
                var file = openFile.FileName;

                var packname = Path.GetFileNameWithoutExtension(file);

                var todir = Path.Combine(Directory.GetCurrentDirectory(), "projects", packname);

                if (Directory.Exists(todir))
                {
                    var result = MessageBox.Show("已存在同名包项目!","警告",MessageBoxButton.YesNo);
                    if (result == MessageBoxResult.Yes)
                        ImportPackToProjects(file);

                }
                else
                {
                    FileFactory.InitDir(todir);
                        ImportPackToProjects(file);

                }

            }
        }

        /// <summary>
        /// 将acpak载入进projects文件夹
        /// </summary>
        public void ImportPackToProjects(string packFile)
        {

            var packname = Path.GetFileNameWithoutExtension(packFile);

            var todir = Path.Combine(Directory.GetCurrentDirectory(), "projects", packname);

            ZipFile.ExtractToDirectory(packFile, todir);

            MessageBox.Show("将acpak载入到projects成功!");

            Load();

        }

    }
}
